<template>
	<el-dialog v-model="visibleInner" title="新增到货检验" width="800px">
		<el-form :model="form" label-width="110px">
			<el-form-item label="检验主题">
				<el-input v-model="form.inspectionSubject" placeholder="请输入" />
			</el-form-item>
			<el-form-item label="检验日期">
				<el-date-picker v-model="form.inspectionDate" type="date" value-format="YYYY-MM-DD" />
			</el-form-item>
			<el-form-item label="检验人ID">
				<el-input-number v-model="form.inspectorId" :min="1" />
			</el-form-item>
			<el-form-item label="供应商ID">
				<el-input-number v-model="form.supplierId" :min="1" />
			</el-form-item>
			<el-form-item label="采购单号">
				<el-input v-model="form.purchaseOrderNumber" placeholder="例如 PO202408190001" />
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remarks" type="textarea" :rows="2" />
			</el-form-item>
			<el-divider>检验明细</el-divider>
			<el-table :data="form.details" size="small" border>
				<el-table-column type="index" label="#" width="50" />
				<el-table-column label="物品编号" width="160">
					<template #default="scope"><el-input v-model="scope.row.itemNumber"/></template>
				</el-table-column>
				<el-table-column label="物品名称" width="180">
					<template #default="scope"><el-input v-model="scope.row.itemName"/></template>
				</el-table-column>
				<el-table-column label="检验类型" width="120">
					<template #default="scope"><el-select v-model="scope.row.inspectionType"><el-option label="全检" value="全检"/><el-option label="抽检" value="抽检"/></el-select></template>
				</el-table-column>
				<el-table-column label="收货/检验/合格/不合格" min-width="320">
					<template #default="scope">
						<el-input-number v-model="scope.row.receivedQuantity" :min="0" /> /
						<el-input-number v-model="scope.row.inspectionQuantity" :min="0" /> /
						<el-input-number v-model="scope.row.qualifiedQuantity" :min="0" /> /
						<el-input-number v-model="scope.row.unqualifiedQuantity" :min="0" />
					</template>
				</el-table-column>
				<el-table-column label="结果" width="120">
					<template #default="scope"><el-select v-model="scope.row.inspectionResult"><el-option label="合格" value="合格"/><el-option label="不合格" value="不合格"/></el-select></template>
				</el-table-column>
			</el-table>
			<div style="margin-top:8px">
				<el-button size="small" @click="addRow" icon="Plus">添加明细</el-button>
			</div>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visibleInner = false">取消</el-button>
				<el-button type="primary" :loading="submitting" @click="submit">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { incomingInspectionApi, type CreateGoodsArrivalInspectionCommand } from '@/api/incomingInspectionApi'

const props = defineProps<{ visible: boolean }>()
const emits = defineEmits(['update:visible','success'])

const visibleInner = ref(false)
watch(() => props.visible, v => visibleInner.value = v)
watch(visibleInner, v => emits('update:visible', v))

const submitting = ref(false)
const form = ref<CreateGoodsArrivalInspectionCommand>({
	inspectionSubject: '',
	isSystemNumber: true,
	inspectionDate: new Date().toISOString().slice(0,10),
	inspectorId: 1,
	supplierId: 1,
	purchaseOrderNumber: '',
	createdBy: 1,
	details: [
		{ serialNumber: 1, itemNumber: '', itemName: '', specificationModel: '', unit: '', brand: '', purchaseQuantity: 0, inspectionType: '全检', receivedQuantity: 0, inspectionQuantity: 0, qualifiedQuantity: 0, unqualifiedQuantity: 0, inspectionResult: '合格', unqualifiedReason: '', inspectionRemarks: '' }
	]
})

const addRow = () => {
	form.value.details.push({ serialNumber: form.value.details.length + 1, itemNumber: '', itemName: '', specificationModel: '', unit: '', brand: '', purchaseQuantity: 0, inspectionType: '全检', receivedQuantity: 0, inspectionQuantity: 0, qualifiedQuantity: 0, unqualifiedQuantity: 0, inspectionResult: '合格', unqualifiedReason: '', inspectionRemarks: '' })
}

const submit = async () => {
	try {
		submitting.value = true
		const res = await incomingInspectionApi.create(form.value)
		if (res.code === 200) {
			ElMessage.success('保存成功')
			visibleInner.value = false
			emits('success')
		} else {
			ElMessage.error(res.message || '保存失败')
		}
	} catch (e:any) {
		ElMessage.error(e.message || '保存失败')
	} finally {
		submitting.value = false
	}
}
</script> 